import itchat
from itchat.content import *

# 自动登录微信（会生成二维码图片）
itchat.auto_login(hotReload=True)

# 创建存储群消息的字典
group_info = {}

@itchat.msg_register(TEXT, isGroupChat=True)
def get_group_msg(msg):
    """
    处理微信群消息
    """
    # 获取群聊ID和名称
    group_id = msg['FromUserName']
    group_name = msg['User']['NickName']
    
    # 记录消息内容
    if group_id not in group_info:
        group_info[group_id] = {
            'group_name': group_name,
            'messages': []
        }
    
    # 添加消息到对应群组
    group_info[group_id]['messages'].append({
        'sender': msg['ActualNickName'],
        'content': msg['Text'],
        'time': msg['CreateTime']
    })
    
    print(f"收到群 [{group_name}] 消息：{msg['Text']}")

def login_and_listen():
    """登录并开始监听消息"""
    itchat.run()

if __name__ == '__main__':
    login_and_listen()
